<?php

require_once 'BlogcommentsView.php';
require_once 'BlogcommentsHandler.php';
require_once 'LoginHandler.php';
require_once 'BlogpostreadView.php';
require_once 'BlogpostsHandler.php';

class BlogcommentsController{
	
	private $ret = "";
	private $message = "";
	private $error = FALSE;
	
	
	public function DoControll(Database $db){
		$blogcommentsView = new BlogcommentsView();
		$blogcommentsHandler = new BlogcommentsHandler($db);
		$blogpostHandler = new BlogpostsHandler($db);
		$blogpostsView = new BlogpostsView($db);
		$loginHandler = new LoginHandler($db);
		$blogpostid = $_GET['id'];
		$id = $_GET['id'];
		
		//skapar kommentarfomuläret om man är inloggad.
		if($loginHandler->IsLoggedIn() === true){
			$this->ret .= $blogcommentsView->BlogComment();
		}//Om man inte är inloggad.
		else {
			$this->ret .= $blogcommentsView->NotLoggedIn();
		}
		//Gör ett försök att raderar kommentaren.
		if($blogcommentsView->TriedToDelete()){
				if($blogcommentsHandler->DeleteBlogcomment($blogcommentsView->DeleteCommentid())){
					$this->message .=$blogcommentsView->Message(BlogcommentsView::DELETED);
				}
			}

		//Skapar delete knappen om admin är inloggad.
		$comments = $blogcommentsHandler->GetSpecificBlogComments($blogpostid);
				
		foreach($comments->getBlogcomments() as $comment){
			$deletebutton = "";
			if($loginHandler->UserIsAdmin() === true){
				$deletebutton = $blogcommentsView->DeleteComment($comment->getId());
			}
			$this->ret .= $blogcommentsView->ShowSpecificBlogComments($comment, $deletebutton);
		}
		
		//Meddelanden när man försöker kommentera.
		if($blogcommentsView->TriedToComment()){
			
			if($loginHandler->IsLoggedIn() === FALSE){
				$this->message .= $blogcommentsView->Message(BlogcommentsView::NOT_LOGGED_IN);
				$this->error = TRUE;
			}
			
			if($blogcommentsHandler->titleIsEmpty($blogcommentsView->GetTitle())){
				$this->message .= $blogcommentsView->Message(BlogcommentsView::TITLE_IS_EMPTY);
				$this->error = TRUE;
			}
			
			if($blogcommentsHandler->commentIsEmpty($blogcommentsView->GetComment())){
				$this->message .= $blogcommentsView->Message(BlogcommentsView::COMMENT_IS_EMPTY);
				$this->error = TRUE;
			}
			//Validerar mot javascript.
			if($blogcommentsHandler->validateTitle($blogcommentsView->GetTitle()) === FALSE){
				$this->message .=$blogcommentsView->Message(BlogcommentsView::NO_HACK);
				$this->error = TRUE;
			}
						
			if($blogcommentsHandler->validateComment($blogcommentsView->GetComment()) === FALSE){
				$this->message .=$blogcommentsView->Message(BlogcommentsView::NO_HACK);
				$this->error = TRUE;
			}
			
			if($this->error === FALSE){
				
				if($blogcommentsHandler->CreateBlogcomments($blogcommentsView->GetTitle(), $blogcommentsView->GetComment(), $blogcommentsView->GetBlogpostid())){
					$this->message .= $blogcommentsView->Message(BlogcommentsView::COMMENT_CREATED);
					
				}
			}
						
		}
		
		
		return $this->ret .$this->message;
	}
	
}
